package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.config.RoleConfig;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.*;
import com.api.shopapi.service.*;
import com.api.shopapi.util.Util;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 护士
 */
@RestController
@RequestMapping("Nurse")
public class NurseController extends BaseController {

    @Autowired
    @Qualifier("NurseServiceImplOne")
    private NurseService nurseService;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;


    @Autowired
    @Qualifier("RoleServiceImplOne")
    private RoleService roleService;


    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;


    @Autowired
    @Qualifier("NurseStaffRelServiceImplOne")
    private NurseStaffRelService nurseStaffRelService;


    @Autowired
    @Qualifier("InstitutionServiceImplOne")
    private InstitutionService institutionService;


    @Autowired
    @Qualifier("UserFaceServiceImplOne")
    private UserFaceService userFaceService;


    //政府管理员获取面板数据
    @RequestMapping(value = "PannelVisual", method = RequestMethod.POST)
    public MyResult PannelVisual(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String governmentCode = jsonObject.getString("governmentCode");


            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Nurse/PannelVisual");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            List<NurseDTO> list = nurseService.listByGovCode(governmentCode);

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("nurseCount",list.size());
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Nurse/PannelVisual", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Insert(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String loginName = jsonObject.getString("loginName");
            String password = jsonObject.getString("password");
            String phone = jsonObject.getString("phone");
            String name = jsonObject.getString("name");
            String idCard=jsonObject.getString("idCard");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/Nurse/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            Staff staff = staffService.getByStaffCode(institutionStaffRel.getStaffCode());

            User user = userService.getByLoginName(loginName);
            if(user!=null){
                return failed("登陆号已注册");
            }


            user = new User();
            String userCode = UUID.randomUUID().toString();
            user.setUserCode(userCode);
            user.setLoginName(loginName);
            user.setPhone(phone);
            user.setName(name);
            user.setPassword(getMD5Str(password));
            user.setCreateTime(new Date());
            user.setUserType(8);
            user.setStatus(1);
            user.setInstitutionCode(institutionCode);
            user.setGovernmentCode(staff.getGovernmentCode());

            Nurse nurse = new Nurse();
            nurse.setNurseCode(UUID.randomUUID().toString());
            nurse.setUserCode(userCode);
            nurse.setIdCard(idCard);
            nurse.setInstitutionCode(institutionCode);

            nurseService.regInsert(user,nurse);

            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/Nurse/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    @RequestMapping(value = "Edit", method = RequestMethod.POST)
    public MyResult update(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String nurseCode = jsonObject.getString("nurseCode");
            String name = jsonObject.getString("name");
            String phone = jsonObject.getString("phone");
            int status = jsonObject.getInteger("status");
            String idCard=jsonObject.getString("idCard");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/Nurse/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Nurse nurse = nurseService.getByNurseCode(nurseCode);
            if(nurse==null){
                return failed("数据不存在");
            }

            User user = userService.getByUserCode(nurse.getUserCode());
            user.setName(name);
            user.setStatus(status);
            user.setPhone(phone);

            nurse.setIdCard(idCard);

            nurseService.updateByNurseCode(user,nurse);

            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/Nurse/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //ins
    @RequestMapping(value = "Page", method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int status = jsonObject.getInteger("status");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/Nurse/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            PageInfo<NurseDTO> pageInfo = nurseService.getPageList(pageIndex,pageSize,institutionCode,name,status);

            for(NurseDTO medicinerDTO:pageInfo.getList()){
                User user = userService.getByUserCode(medicinerDTO.getUserCode());
                medicinerDTO.setName(user.getName());
                medicinerDTO.setPhone(user.getPhone());
                medicinerDTO.setStatus(user.getStatus());
                medicinerDTO.setLoginName(user.getLoginName());
                medicinerDTO.setCreateTime(user.getCreateTime());

                List<NurseStaffRelDTO> listDTO =
                        nurseStaffRelService.getListByNurseCode(medicinerDTO.getNurseCode());
                for(NurseStaffRelDTO medicinerStaffRelDTO:listDTO){

                    Nurse mediciner = nurseService.getByNurseCode(medicinerStaffRelDTO.getNurseCode());
                    User user2 = userService.getByUserCode(mediciner.getUserCode());
                    medicinerStaffRelDTO.setNurseName(user2.getName());

                    Staff staff1 = staffService.getByStaffCode(medicinerStaffRelDTO.getStaffCode());
                    medicinerStaffRelDTO.setStaffName(staff1.getLoginName());

                }
                medicinerDTO.setListNurseStaffRelDTO(listDTO);

            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo);

            return success(jsonObject1);
        }  catch (Exception e) {
            insertError("/Nurse/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //ins
    @RequestMapping(value = "InsPageByInsCode", method = RequestMethod.POST)
    public MyResult InsPageByInsCode(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String institutionCode = jsonObject.getString("institutionCode");

            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Nurse/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            PageInfo<NurseDTO> pageInfo = nurseService.getPageList(pageIndex,pageSize,institutionCode,"",-1);

            for(NurseDTO medicinerDTO:pageInfo.getList()){
                User user = userService.getByUserCode(medicinerDTO.getUserCode());
                medicinerDTO.setName(user.getName());
                medicinerDTO.setPhone(user.getPhone());
                medicinerDTO.setStatus(user.getStatus());
                medicinerDTO.setLoginName(user.getLoginName());
                medicinerDTO.setCreateTime(user.getCreateTime());

                List<NurseStaffRelDTO> listDTO =
                        nurseStaffRelService.getListByNurseCode(medicinerDTO.getNurseCode());
                for(NurseStaffRelDTO medicinerStaffRelDTO:listDTO){

                    Nurse mediciner = nurseService.getByNurseCode(medicinerStaffRelDTO.getNurseCode());
                    User user2 = userService.getByUserCode(mediciner.getUserCode());
                    medicinerStaffRelDTO.setNurseName(user2.getName());

                    Staff staff1 = staffService.getByStaffCode(medicinerStaffRelDTO.getStaffCode());
                    medicinerStaffRelDTO.setStaffName(staff1.getLoginName());

                }
                medicinerDTO.setListNurseStaffRelDTO(listDTO);

            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo);

            return success(jsonObject1);
        }  catch (Exception e) {
            insertError("/Nurse/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //ins
    @RequestMapping(value = "InsPageApp", method = RequestMethod.POST)
    public MyResult InsPageApp(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            String name = jsonObject.getString("name");
            int status = jsonObject.getInteger("status");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Nurse/InsPageApp");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Institution institution = institutionService.getByUserCode(userCode);
            String institutionCode = institution.getInstitutionCode();

            PageInfo<NurseDTO> pageInfo = nurseService.getPageList(pageIndex,pageSize,institutionCode,name,status);

            for(NurseDTO nurseDTO:pageInfo.getList()){
                User user = userService.getByUserCode(nurseDTO.getUserCode());
                nurseDTO.setName(user.getName());
                nurseDTO.setPhone(user.getPhone());
                nurseDTO.setStatus(user.getStatus());
                nurseDTO.setLoginName(user.getLoginName());
                nurseDTO.setCreateTime(user.getCreateTime());

                UserFace userFace = userFaceService.getByUserCode(nurseDTO.getUserCode());
                if(userFace == null){
                    nurseDTO.setIsRegFace(false);
                }else{
                    nurseDTO.setIsRegFace(true);
                }

            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo);

            return success(jsonObject1);
        }  catch (Exception e) {
            insertError("/Nurse/InsPageApp", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //ins
    @RequestMapping(value = "InsDelete", method = RequestMethod.POST)
    public MyResult InsDelete(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/Mediciner/InsDelete");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            List<Nurse> list = nurseService.listByInsCode(institutionCode);
            for(Nurse nurse:list){

                nurseService.delete(nurse.getNurseCode());

                //删除老的关系
                List<NurseStaffRelDTO> listStaffRel = nurseStaffRelService.getListByNurseCode(nurse.getNurseCode());
                for(NurseStaffRelDTO medicinerStaffRelDTO : listStaffRel){
                    nurseStaffRelService.delete(medicinerStaffRelDTO.getNurseStaffRelCode());
                }

            }


            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }  catch (Exception e) {
            insertError("/Mediciner/InsDelete", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //gov
    @RequestMapping(value = "GovPage", method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int status = jsonObject.getInteger("status");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/Nurse/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<NurseDTO> pageInfo = nurseService.pageByGovCode(pageIndex,pageSize, staff.getGovernmentCode());

            for(NurseDTO medicinerDTO:pageInfo.getList()){
                User user = userService.getByUserCode(medicinerDTO.getUserCode());
                medicinerDTO.setName(user.getName());
                medicinerDTO.setPhone(user.getPhone());
                medicinerDTO.setStatus(user.getStatus());
                medicinerDTO.setLoginName(user.getLoginName());
                medicinerDTO.setCreateTime(user.getCreateTime());

                List<NurseStaffRelDTO> listDTO =
                        nurseStaffRelService.getListByNurseCode(medicinerDTO.getNurseCode());
                for(NurseStaffRelDTO medicinerStaffRelDTO:listDTO){

                    Nurse mediciner = nurseService.getByNurseCode(medicinerStaffRelDTO.getNurseCode());
                    User user2 = userService.getByUserCode(mediciner.getUserCode());
                    medicinerStaffRelDTO.setNurseName(user2.getName());

                    Staff staff1 = staffService.getByStaffCode(medicinerStaffRelDTO.getStaffCode());
                    medicinerStaffRelDTO.setStaffName(staff1.getLoginName());

                }
                medicinerDTO.setListNurseStaffRelDTO(listDTO);

            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo);

            return success(jsonObject1);
        }  catch (Exception e) {
            insertError("/Nurse/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }
    @RequestMapping(value = "GovPage2", method = RequestMethod.POST)
    public MyResult GovPage2(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String institutionCode = jsonObject.getString("institutionCode");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/Nurse/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<Nurse> pageInfo = nurseService.pageByGovCodeInsCode(pageIndex,pageSize,
                    staff.getGovernmentCode(),institutionCode);

            List<NurseDTO> listDTO2 = new ArrayList<>();
            for(Nurse nurse:pageInfo.getList()){
                nurse = nurseService.getByNurseCode(nurse.getNurseCode());
                NurseDTO nurseDTO = mapper.map(nurse,NurseDTO.class);

                User user = userService.getByUserCode(nurse.getUserCode());
                nurseDTO.setName(user.getName());
                nurseDTO.setPhone(user.getPhone());
                nurseDTO.setStatus(user.getStatus());
                nurseDTO.setLoginName(user.getLoginName());
                nurseDTO.setCreateTime(user.getCreateTime());

                List<NurseStaffRelDTO> listDTO =
                        nurseStaffRelService.getListByNurseCode(nurseDTO.getNurseCode());
                for(NurseStaffRelDTO medicinerStaffRelDTO:listDTO){

                    Nurse mediciner = nurseService.getByNurseCode(medicinerStaffRelDTO.getNurseCode());
                    User user2 = userService.getByUserCode(mediciner.getUserCode());
                    medicinerStaffRelDTO.setNurseName(user2.getName());

                    Staff staff1 = staffService.getByStaffCode(medicinerStaffRelDTO.getStaffCode());
                    medicinerStaffRelDTO.setStaffName(staff1.getLoginName());

                }
                nurseDTO.setListNurseStaffRelDTO(listDTO);

                listDTO2.add(nurseDTO);

            }

            pageInfo.setList(Collections.unmodifiableList(listDTO2));

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo);

            return success(jsonObject1);
        }  catch (Exception e) {
            insertError("/Nurse/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult list(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/Nurse/List");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            List<NurseDTO> nurseDTOList = nurseService.getList(institutionCode);
            return success(nurseDTOList);
        } catch (Exception e) {
            insertError("/Nurse/List", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }





    @RequestMapping(value = "AddForOut", method = RequestMethod.POST)
    public MyResult AddForOut(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            JSONArray nurseArray = jsonObject.getJSONArray("nurseArray");
            String institutionCode = jsonObject.getString("institutionCode");

            if (!checkSignForOut(timeStr, sign)) {
                return failed("签名错误");
            }

            Institution institution = institutionService.getByInstitutionCode(institutionCode);
            Role role = roleService.getByName(RoleConfig.nurseRoleName);

            HashMap<String,String> hashMapPhone = new HashMap<>();
            HashMap<String,String> hashMapIdCard = new HashMap<>();
            for(int i=0;i<nurseArray.size();i++){

                String name = nurseArray.getJSONObject(i).getString("name").trim();
                String phone = nurseArray.getJSONObject(i).getString("phone").trim();
                String idCard = nurseArray.getJSONObject(i).getString("idCard").trim();

                if(!phone.equals("") && phone.length() !=11){
                    return failed("手机号格式错误"+phone);
                }
                if(phone.equals("")){
                    phone = getPhoneAuto(hashMapPhone);
                }

                if(hashMapPhone.containsKey(phone)){
                    return failed("手机号存在重复的"+phone);
                }else{
                    hashMapPhone.put(phone,phone);
                }
                if(hashMapIdCard.containsKey(idCard)){
                    return failed("身份证号存在重复的"+idCard);
                }else{
                    hashMapIdCard.put(idCard,phone);
                }


                if(name.equals("")){
                    return failed("名称不能为空");
                }
                if(!isPhone(phone)){
                    return failed("手机格式错误");
                }
                if(!isIdCard(idCard)){
                    return failed("身份证格式错误");
                }

                User user = userService.getByPhone(phone);
                if(user != null){
                    return failed("手机号已使用："+phone);
                }


            }

            Date date = new Date();
            for(int i=0;i<nurseArray.size();i++){

                String name = nurseArray.getJSONObject(i).getString("name");
                //String phone = nurseArray.getJSONObject(i).getString("phone");
                String idCard = nurseArray.getJSONObject(i).getString("idCard");

                String phone = hashMapIdCard.get(idCard);

                User user = new User();
                user.setUserCode(UUID.randomUUID().toString());
                user.setName(name);
                user.setLoginName(phone);
                user.setPhone(phone);
                user.setPassword(getMD5Str(phone.substring(5,11)));
                user.setUserType(8);
                user.setStatus(1);
                user.setGovernmentCode(institution.getGovernmentCode());
                user.setInstitutionCode(institutionCode);
                user.setCreateTime(date);
                userService.insert(user);


                Nurse nurse = new Nurse();
                nurse.setNurseCode(UUID.randomUUID().toString());
                nurse.setUserCode(user.getUserCode());
                nurse.setIdCard(idCard);
                nurse.setInstitutionCode(institutionCode);
                nurse.setCreateTime(date);
                nurseService.insert(nurse);

//                Staff staff3 = new Staff();
//                staff3.setStaffCode(UUID.randomUUID().toString());
//                staff3.setGovernmentCode(institution.getGovernmentCode());
//                staff3.setRoleCode(role.getRoleCode());
//                staff3.setLoginName(idCard);
//                staff3.setPassword(getMD5Str(idCard.substring(1,7)));
//                staff3.setRealName(name);
//                staff3.setPhone(phone);
//                staff3.setCreateTime(date);
//                staffService.insert(staff3);
//
//                //删除老的关系
//                List<NurseStaffRelDTO> listStaffRel = nurseStaffRelService.getListByNurseCode(nurse.getNurseCode());
//                for(NurseStaffRelDTO nurseStaffRelDTO : listStaffRel){
//                    nurseStaffRelService.delete(nurseStaffRelDTO.getNurseStaffRelCode());
//                }
//
//                //增加新的关系
//                NurseStaffRel medicinerStaffRel = new NurseStaffRel();
//                medicinerStaffRel.setNurseStaffRelCode(UUID.randomUUID().toString());
//                medicinerStaffRel.setNurseCode(nurse.getNurseCode());
//                medicinerStaffRel.setStaffCode(staff3.getStaffCode());
//                nurseStaffRelService.insert2(medicinerStaffRel);

            }

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);

        } catch (Exception e) {
            insertError("/Nurse/AddForOut",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }



    //生成登陆手机号
    private String getPhoneAuto(HashMap<String,String> hashMapPhone){

        String newPhone = "100"+random8();

        if(hashMapPhone.containsKey(newPhone)){
            newPhone = getPhoneAuto(hashMapPhone);
        }

        User user = userService.getByPhone(newPhone);
        if(user != null){
            newPhone = getPhoneAuto(hashMapPhone);
        }

        return newPhone;
    }

    /**
     * * 生成8位随机数
     **/
    private int random8() {
        Random r = new Random();
        StringBuffer sb = new StringBuffer();
        int[] c = new int[8];
        for (int i = 0; i < 8; i++) {
            c[i] = r.nextInt(9) + 1;
            sb.append(c[i]);
        }
        return Integer.parseInt(sb.toString());
    }



}
